package org.sql.jdbc.utils;

import java.text.SimpleDateFormat;
import java.util.List;

import org.sql.jdbc.constants.DbTypeEnum;
import org.sql.jdbc.id.UniqueIdUtil;
import org.sql.jdbc.param.Operator;
import org.sql.jdbc.param.Param;

/** 
 * sql工具
 *
 * <pre> 
 * 构建组：e-jdbc
 * 作者：eddy
 * 邮箱：1546077710@qq.com
 * 日期：2017年4月24日-下午9:11:52
 * 版权：E源时代
 * </pre>
 */
public class SqlUtil {

	public static String getInsertSql(Operator operator){
		StringBuilder sqlBuilder = new StringBuilder();
		String pkValue = null;
		Double pks = 0.0D;

		List<Param> fieldLis = operator.getFieldLis();
		Param pk = operator.getPrikey();
		String tabName = operator.getTabName();
		
		sqlBuilder.append("insert into ");
		StringBuilder valueSql = new StringBuilder();
		valueSql.append("values (");
		//获取数据表名
		sqlBuilder.append(tabName+" (");
		//主键
		String fieldPk  = pk.getParamField();
		sqlBuilder.append(fieldPk+",");
		
		if(null != pk.getParamValue() 
				&& !pk.getParamValue().equals("")){
			if(pk.getParamType().equals("java.lang.Double")){
				pks =Double.valueOf(pk.getParamValue());
				pkValue = pk.getParamValue();
			}else{
				pkValue = pk.getParamValue();
			}
		}else {
			//生成主键
			pkValue = UniqueIdUtil.getSID();
		}
		
		if(pk.getParamType().equals("java.lang.Double")){
			valueSql.append(pks+",");
		}else{
			valueSql.append("'"+pkValue+"',");
		}
		
		//获取OBJECT对应数据库字段
		for(int i=0;i<fieldLis.size();i++){
			Param field = fieldLis.get(i);
			sqlBuilder.append(field.getParamField());
			valueSql.append("?");
			if((i+1) < fieldLis.size()){
				sqlBuilder.append(","); 
				valueSql.append(",");
			}
		}
		sqlBuilder.append(") ");
		valueSql.append(")");
		sqlBuilder.append(valueSql);
		
		operator.getPrikey().setParamValue(pkValue);
		
		return sqlBuilder.toString();
	}
	
	public static String getUpdateSql(Operator operator){
		StringBuilder sqlBuilder = new StringBuilder();
		
		List<Param> fieldLis = operator.getFieldLis();
		Param pk = operator.getPrikey();
		String tabName = operator.getTabName();
		
		sqlBuilder.append("update ");
		sqlBuilder.append(tabName+" set ");
		for(int i=0;i<fieldLis.size();i++){
			Param field = fieldLis.get(i);
			  String fieldName = field.getParamField();
			  sqlBuilder.append(fieldName);
			  sqlBuilder.append("=?");
			  if((i+1) < fieldLis.size()){
				  sqlBuilder.append(",");
			  }
		}
		//主键
		sqlBuilder.append(" where "+pk.getParamField());
		sqlBuilder.append("='"+String.valueOf(pk.getParamValue())+"'");
		
		return sqlBuilder.toString();
	}
	
	/**
	 * sql语句填值
	 *
	 * @param sql
	 * @param param
	 * @return
	 * @throws Exception 
	 */
	public static String fillSql(String dbType, String sql, List<Object> param) throws Exception{
		if(null == sql || "".equals(sql) || 0 == sql.length()){
			throw new Exception("SQL语句为空！");
		}
		if(null != param && param.size() > 0){
			for (int i = 0; i < param.size();i++) {
				if(param.get(i) instanceof String){
					sql = sql.replace("?", "'"+ param.get(i) +"'");
				}else if(param.get(i) instanceof java.util.Date
						|| param.get(i) instanceof java.sql.Date){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
					if(dbType.equals(DbTypeEnum.ORACLE.code())){
						sql = sql.replace("?", "to_date("+ sdf.format((java.util.Date)param.get(i)) 
							+",'yyyymmdd hh24:mi:ss')");
					}else{
						sql = sql.replace("?", sdf.format((java.util.Date)param.get(i)));
					}
				}else{
					sql = sql.replace("?", param.get(i).toString());
				}
			}
		}
		return sql;
	}
}
